
public class lists 
{	
	Element head = null;
	
	public void addend (Element elem)
	{
		if (head == null)
		{
			head = elem;
		}
		else
		{
			Element cur = head;
			while (cur.next != null)
			{
				cur = cur.next;
			}
			cur.next = elem;
		}
	}
	
	public void addbeg (Element elem)
	{
		elem.next = head;
		head = elem;
	}
	
	public void addafter (Element elem, Element aelem)
	{	
		if(head==null)
			head = elem;
		else
		{
			Element cur = head;
			while (cur.i != aelem.i)
			{
				cur = cur.next;
			}
			elem.next = cur.next;
			cur.next = elem;
			cur = cur.next;
		}
	}
	
	public void addbefore (Element elem, Element belem)
	{
		if (head.i==elem.i)
		{
			addbeg(belem);
		}
		else
		{
			Element cur = head;
	
			while (cur.next.i != belem.i)
			{
				cur = cur.next;
			}
			elem.next = cur.next;
			cur.next = elem;
		}
	}
	
	public void delelem (Element elem)
	{
		if(head.i == elem.i)
		{
			head = head.next;
		}
		else
		{
			Element cur = head;
			while (cur.next.i != elem.i)
			{
				cur = cur.next;
			}
			cur.next = cur.next.next;
		}
	}
	
	public int howmuch()
	{
		int count=0;
		
		Element current = head;
		
		while (current != null)
		{
			current=current.next;
			count++;
		}
		return count;
	}
	
	public void search (int i)
	{
		Element current = head;
		
		while (current != null)
		{
			if (current.i == i)
			{
				System.out.println("Element " + i + " was found");
				return;
			}
			else
			{
				current=current.next;
			}
		}
		System.out.println ("Element " + i + " was not found");
	}
	
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("{ ");
		Element current = head;
		while (current != null)
		{
			sb.append (current.i + " ");
			current = current.next;
		}
		sb.append("}");
		return sb.toString();
	}
}
